import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
var _excluded = ["dataSource", "processing", "processingDeleteRow", "processingEnableRow", "levelCode", "menuTypeList", "expandClick"];

/**
 * Table - 菜单配置 - 列表页面表格
 * @date: 2018-7-4
 */
import React, { PureComponent } from 'react';
import { Icon, Table } from 'antd';
import Icons from 'components/Icons';
import { Button as ButtonPermission } from 'components/Permission';
import intl from 'utils/intl';
import { enableRender, TagRender, operatorRender } from 'utils/renderer';
import { isTenantRoleLevel, tableScrollWidth } from 'utils/utils';
var viewButtonPrompt = 'ciam.menuConfig.view.button';
var modelPrompt = 'ciam.menuConfig.model.menuConfig';
var commonPrompt = 'cpaas.common';
var tenantRoleLevel = isTenantRoleLevel();
var menuIconStyle = {
  width: 14,
  height: 14,
  lineHeight: '14px'
};

var List = /*#__PURE__*/function (_PureComponent) {
  _inherits(List, _PureComponent);

  var _super = _createSuper(List);

  function List() {
    var _this;

    _classCallCheck(this, List);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.defaultTableRowKey = 'id';
    return _this;
  }

  _createClass(List, [{
    key: "onCell",
    value: function onCell() {
      return {
        style: {
          overflow: 'hidden',
          maxWidth: 220,
          textOverflow: 'ellipsis',
          whiteSpace: 'nowrap'
        },
        onClick: function onClick(e) {
          var target = e.target;

          if (target.style.whiteSpace === 'normal') {
            target.style.whiteSpace = 'nowrap';
          } else {
            target.style.whiteSpace = 'normal';
          }
        }
      };
    }
  }, {
    key: "operationRender",
    value: function operationRender(text, record) {
      // 租户级应该能看到他的菜单挂载到了哪个目录下了，但是，需要限制，租户层功能只能看到标准菜单，但是不能做任何更改（没有任何操作按钮）
      if (tenantRoleLevel && record.customFlag === 0) {
        return null;
      }

      var _this$props = this.props,
          path = _this$props.path,
          processingEnableRow = _this$props.processingEnableRow,
          _this$props$processin = _this$props.processing,
          processing = _this$props$processin === void 0 ? {} : _this$props$processin,
          _this$props$handleEdi = _this$props.handleEdit,
          handleEdit = _this$props$handleEdi === void 0 ? function (e) {
        return e;
      } : _this$props$handleEdi,
          _this$props$handleEna = _this$props.handleEnable,
          handleEnable = _this$props$handleEna === void 0 ? function (e) {
        return e;
      } : _this$props$handleEna,
          _this$props$handleEdi2 = _this$props.handleEditPermissionSet,
          handleEditPermissionSet = _this$props$handleEdi2 === void 0 ? function (e) {
        return e;
      } : _this$props$handleEdi2,
          _this$props$onCreate = _this$props.onCreate,
          onCreate = _this$props$onCreate === void 0 ? function (e) {
        return e;
      } : _this$props$onCreate,
          _this$props$onCopy = _this$props.onCopy,
          onCopy = _this$props$onCopy === void 0 ? function (e) {
        return e;
      } : _this$props$onCopy;
      var operators = [];

      if (record.type === 'root' || record.type === 'dir') {
        operators.push({
          key: 'create',
          ele: /*#__PURE__*/React.createElement(ButtonPermission, {
            type: "text",
            permissionList: [{
              code: "".concat(path, ".button.createList"),
              type: 'button',
              meaning: '菜单配置-列表新建'
            }],
            onClick: function onClick() {
              return onCreate(record, true);
            }
          }, intl.get("".concat(commonPrompt, ".button.create")).d('新建')),
          len: 2,
          title: intl.get("".concat(commonPrompt, ".button.create")).d('新建')
        });
      }

      operators.push({
        key: 'edit',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.editList"),
            type: 'button',
            meaning: '菜单配置-列表编辑'
          }],
          onClick: function onClick() {
            return handleEdit(record);
          }
        }, intl.get("".concat(commonPrompt, ".button.edit")).d('编辑')),
        len: 2,
        title: intl.get("".concat(commonPrompt, ".button.edit")).d('编辑')
      });

      if (processing.setEnable && record.id === processingEnableRow) {
        operators.push({
          key: 'enable-disabled',
          ele: /*#__PURE__*/React.createElement(ButtonPermission, {
            type: "text",
            permissionList: [{
              code: "".concat(path, ".button.loading"),
              type: 'button',
              meaning: '菜单配置-loading'
            }],
            key: "loading"
          }, /*#__PURE__*/React.createElement(Icon, {
            type: "loading"
          })),
          len: 4
        });
      } else if (record.enabledFlag === 1) {
        operators.push({
          key: 'enable-disabled',
          ele: /*#__PURE__*/React.createElement(ButtonPermission, {
            type: "text",
            permissionList: [{
              code: "".concat(path, ".button.disableList"),
              type: 'button',
              meaning: '菜单配置-列表禁用'
            }],
            onClick: function onClick() {
              return handleEnable(record, 'disable');
            }
          }, intl.get("".concat(commonPrompt, ".status.disable")).d('禁用')),
          len: 2,
          title: intl.get("".concat(commonPrompt, ".status.disable")).d('禁用')
        });
      } else {
        operators.push({
          key: 'enable-disabled',
          ele: /*#__PURE__*/React.createElement(ButtonPermission, {
            type: "text",
            permissionList: [{
              code: "".concat(path, ".button.enableList"),
              type: 'button',
              meaning: '菜单配置-列表启用'
            }],
            onClick: function onClick() {
              return handleEnable(record, 'enable');
            }
          }, intl.get("".concat(commonPrompt, ".status.enable")).d('启用')),
          len: 2,
          title: intl.get("".concat(commonPrompt, ".status.enable")).d('启用')
        });
      }

      if (['menu', 'inner-link', 'link', 'window'].includes(record.type)) {
        operators.push({
          key: 'permission',
          ele: /*#__PURE__*/React.createElement(ButtonPermission, {
            type: "text",
            permissionList: [{
              code: "".concat(path, ".button.permissionSet"),
              type: 'button',
              meaning: '菜单配置-列表权限集'
            }],
            onClick: function onClick() {
              return handleEditPermissionSet(record);
            }
          }, intl.get("".concat(viewButtonPrompt, ".button.permissionSet")).d('权限集')),
          len: 3,
          title: intl.get("".concat(viewButtonPrompt, ".button.permissionSet")).d('权限集')
        });
      }

      operators.push({
        key: 'permission',
        ele: /*#__PURE__*/React.createElement(ButtonPermission, {
          type: "text",
          permissionList: [{
            code: "".concat(path, ".button.copy"),
            type: 'button',
            meaning: '菜单配置-复制菜单'
          }],
          onClick: function onClick() {
            return onCopy(record);
          }
        }, intl.get("".concat(commonPrompt, ".button.copy")).d('复制'))
      });
      return operatorRender(operators, record, {
        limit: 3
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          _this$props2$dataSour = _this$props2.dataSource,
          dataSource = _this$props2$dataSour === void 0 ? [] : _this$props2$dataSour,
          _this$props2$processi = _this$props2.processing,
          processing = _this$props2$processi === void 0 ? {} : _this$props2$processi,
          processingDeleteRow = _this$props2.processingDeleteRow,
          processingEnableRow = _this$props2.processingEnableRow,
          _this$props2$levelCod = _this$props2.levelCode,
          levelCode = _this$props2$levelCod === void 0 ? [] : _this$props2$levelCod,
          _this$props2$menuType = _this$props2.menuTypeList,
          menuTypeList = _this$props2$menuType === void 0 ? [] : _this$props2$menuType,
          _this$props2$expandCl = _this$props2.expandClick,
          expandClick = _this$props2$expandCl === void 0 ? function (e) {
        return e;
      } : _this$props2$expandCl,
          others = _objectWithoutProperties(_this$props2, _excluded);

      var filteredMenuTypeList = menuTypeList.filter(function (item) {
        return item.value !== 'ps';
      });
      var columns = [{
        title: intl.get("".concat(modelPrompt, ".name")).d('目录/菜单'),
        dataIndex: 'name',
        fixed: 'left',
        width: 200
      }, {
        title: intl.get("".concat(modelPrompt, ".parentName")).d('上级目录'),
        width: 120,
        dataIndex: 'parentName'
      }, {
        title: intl.get("".concat(modelPrompt, ".quickIndex")).d('快速索引'),
        width: 120,
        dataIndex: 'quickIndex'
      }, {
        title: intl.get("".concat(modelPrompt, ".icon")).d('图标'),
        width: 60,
        dataIndex: 'icon',
        render: function render(code) {
          return /*#__PURE__*/React.createElement(Icons, {
            type: code,
            size: 14,
            style: menuIconStyle
          });
        }
      }, {
        title: intl.get("".concat(modelPrompt, ".code")).d('编码'),
        dataIndex: 'code',
        onCell: this.onCell.bind(this)
      }, !tenantRoleLevel && {
        title: intl.get("".concat(modelPrompt, ".level")).d('层级'),
        dataIndex: 'level',
        width: 80,
        render: function render(text) {
          var levelMeaning = '';
          levelCode.filter(function (o) {
            return o.value !== 'org';
          }).forEach(function (element) {
            if (text === element.value) {
              levelMeaning = element.meaning;
            }
          });
          return levelMeaning;
        }
      }, {
        title: intl.get("".concat(modelPrompt, ".menuType")).d('类型'),
        dataIndex: 'type',
        width: 120,
        render: function render(value) {
          var statusList = filteredMenuTypeList.map(function (item) {
            return {
              status: item.value,
              color: item.value === 'root' ? 'blue' : item.value === 'dir' ? 'green' : 'orange',
              text: item.meaning
            };
          });
          return TagRender(value, statusList);
        }
      }, {
        title: intl.get("".concat(modelPrompt, ".sort")).d('序号'),
        dataIndex: 'sort',
        width: 60
      }, {
        title: intl.get("".concat(modelPrompt, ".description")).d('描述'),
        dataIndex: 'description',
        width: 200,
        onCell: this.onCell.bind(this)
      }, {
        title: intl.get("".concat(commonPrompt, ".status")).d('状态'),
        dataIndex: 'enabledFlag',
        width: 120,
        fixed: 'right',
        render: enableRender
      }, {
        title: intl.get("".concat(commonPrompt, ".table.column.option")).d('操作'),
        width: 200,
        fixed: 'right',
        render: this.operationRender.bind(this)
      }].filter(Boolean);

      var tableProps = _objectSpread({
        columns: columns,
        dataSource: dataSource,
        pagination: false,
        bordered: true,
        rowKey: this.defaultTableRowKey,
        loading: processing.query,
        childrenColumnName: 'subMenus',
        scroll: {
          x: tableScrollWidth(columns)
        },
        onExpand: expandClick
      }, others);

      return /*#__PURE__*/React.createElement(Table, tableProps);
    }
  }]);

  return List;
}(PureComponent);

export { List as default };